{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "filled-possession",
   "metadata": {},
   "source": [
    "# 线性代数（torch基础）\n",
    "\n",
    "视频地址：https://www.bilibili.com/video/BV1eK4y1U7Qy?p=2\n",
    "\n",
    "`import torch`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "civic-campbell",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([0, 1, 2, 3])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "x = torch.arange(4)\n",
    "\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "curious-minimum",
   "metadata": {},
   "source": [
    "## 访问张量\n",
    "\n",
    ">通过索引访问张量一个元素：\n",
    ">>代码：`张量变量.[数字下标]`\n",
    "\n",
    ">访问张量长度：\n",
    ">>代码：`len(张量变量)`  \n",
    "\n",
    ">访问张量形状：\n",
    ">>代码：`张量变量.shape`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "wooden-google",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(3)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问张量元素\n",
    "x[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "confused-sherman",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问张量长度\n",
    "len(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cellular-controversy",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 2, 3])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 访问张量形状\n",
    "\n",
    "y = torch.tensor([\n",
    "    [[1,2,3],[4,5,6]],\n",
    "    [[7,9,0],[1,2,4]]\n",
    "])\n",
    "\n",
    "y.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "saved-professional",
   "metadata": {},
   "source": [
    "## 矩阵操作\n",
    "\n",
    ">创建矩阵：\n",
    ">>代码：`torch.tensor([ [], [] ])`  \n",
    ">>说明：最内层为行，接着为列\n",
    "\n",
    ">指定分量大小创建矩阵：\n",
    ">>代码：`张量变量.reshape(最外层元素个数, 内层元素个数)`\n",
    "\n",
    ">矩阵转置：\n",
    ">>代码：`矩阵变量.T`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "lined-guinea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11],\n",
       "        [12, 13, 14, 15],\n",
       "        [16, 17, 18, 19]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "A = torch.arange(20).reshape(5,4)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "royal-melbourne",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0,  4,  8, 12, 16],\n",
       "        [ 1,  5,  9, 13, 17],\n",
       "        [ 2,  6, 10, 14, 18],\n",
       "        [ 3,  7, 11, 15, 19]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "monthly-prediction",
   "metadata": {},
   "source": [
    "## 矩阵运算\n",
    "\n",
    ">矩阵克隆\n",
    ">>代码：`矩阵变量B = 矩阵变量A.clone()`  \n",
    ">>说明：通过分配新内存，为矩阵生成副本\n",
    "\n",
    ">两个矩阵运算\n",
    ">>两矩阵对应元素相加：`矩阵变量A + 矩阵变量 B`  \n",
    ">>两矩阵对应元素相乘：`矩阵变量A * 矩阵变量 B`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "invisible-pillow",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A: tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.],\n",
      "        [12., 13., 14., 15.],\n",
      "        [16., 17., 18., 19.]])\n",
      "B: tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.],\n",
      "        [12., 13., 14., 15.],\n",
      "        [16., 17., 18., 19.]])\n"
     ]
    }
   ],
   "source": [
    "A = torch.arange(20, dtype=torch.float32).reshape(5,4)\n",
    "B = A.clone()\n",
    "\n",
    "print('A:', A)\n",
    "print('B:', B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "comparable-moral",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.,  2.,  4.,  6.],\n",
       "        [ 8., 10., 12., 14.],\n",
       "        [16., 18., 20., 22.],\n",
       "        [24., 26., 28., 30.],\n",
       "        [32., 34., 36., 38.]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 两矩阵对应元素相加\n",
    "A + B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "qualified-imagination",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[  0.,   1.,   4.,   9.],\n",
       "        [ 16.,  25.,  36.,  49.],\n",
       "        [ 64.,  81., 100., 121.],\n",
       "        [144., 169., 196., 225.],\n",
       "        [256., 289., 324., 361.]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 两矩阵对应元素相乘\n",
    "A * B"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "persistent-forth",
   "metadata": {},
   "source": [
    "## 降维\n",
    "\n",
    ">张量元素求和\n",
    ">>代码：`张量变量.sum()`\n",
    "\n",
    ">沿轴通过求和降低张量维度\n",
    ">>汇总列元素降维：`张量变量.sum(axis=0)`  \n",
    ">>汇总行元素降维：`张量变量.sum(axis=1)`  \n",
    ">>对矩阵的所有元素进⾏求和:`张量变量.sum(axis=[0,1])` \n",
    "\n",
    ">非降维求和\n",
    ">>代码：`张量变量.sum(axis=0, keepdims=True)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "modified-possibility",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([0., 1., 2., 3.]), tensor(6.))"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "x = torch.arange(4, dtype=torch.float32)\n",
    "\n",
    "x, x.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "noticed-hours",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.],\n",
      "        [12., 13., 14., 15.],\n",
      "        [16., 17., 18., 19.]])\n",
      "tensor([40., 45., 50., 55.])\n",
      "tensor([ 6., 22., 38., 54., 70.])\n",
      "tensor([[ 6.],\n",
      "        [22.],\n",
      "        [38.],\n",
      "        [54.],\n",
      "        [70.]])\n"
     ]
    }
   ],
   "source": [
    "A = torch.arange(20, dtype=torch.float32).reshape(5,4)\n",
    "print(A)\n",
    "\n",
    "x = A.sum(axis=0)\n",
    "print(x)\n",
    "\n",
    "y = A.sum(axis=1)\n",
    "print(y)\n",
    "\n",
    "z = A.sum(axis=1, keepdims=True)\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sorted-china",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
